﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BUS;
using DTO;
namespace QLDT
{
    public partial class frm_DMLop : DevExpress.XtraEditors.XtraForm
    {
        public bool TTThemSua = true;
        private string malop_cu;
        public frm_DMLop()
        {
            InitializeComponent();
        }

        private void textEdit1_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void frm_DMLop_Load(object sender, EventArgs e)
        {
            load();
        }
        public void load()
        {
            layoutKhoahoc_txt.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
            layoutKhoahoc_lookup.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
            grid_DMLop.DataSource = BUS_Lop.SelectLop();
            txt_DMHeDT.Enabled = false;
            txt_DMMaKH.Enabled = false;
            txt_DMSiSo.Enabled = false;
            txt_DMMaLop.Enabled = false;
            txt_DMTenLop.Enabled = false;
            simpleBt_CapNhat.Enabled = simpleBt_Huy.Enabled = false;
            if (gv_DML.RowCount < 1)
            {
                simpleBt_Sua.Enabled = simpleBt_Xoa.Enabled = false;
            }
            else
                simpleBt_Sua.Enabled = simpleBt_Xoa.Enabled = true;
            simpleBt_Them.Enabled = true;
            Bindings();
            gridLookKhoahoc.Properties.View.OptionsBehavior.AutoPopulateColumns = false;
            gridLookKhoahoc.Properties.AllowNullInput = DevExpress.Utils.DefaultBoolean.True;
            gridLookKhoahoc.Properties.ValueMember = "MaKH";
            gridLookKhoahoc.Properties.DisplayMember = "MaKH";
            gridLookKhoahoc.Properties.View.BestFitColumns();
            gridLookKhoahoc.Properties.PopupFormWidth = 400;
            gridLookKhoahoc.Properties.NullText = "Chọn khóa hoc";
            gridLookKhoahoc.Properties.DataSource = BUS_KhoaHoc.SelectKhoaHoc();
        }
        public void Bindings()
        {
            txt_DMHeDT.DataBindings.Clear();
            txt_DMHeDT.DataBindings.Add("Text", gv_DML.DataSource, "HeDT");

            txt_DMMaKH.DataBindings.Clear();
            txt_DMMaKH.DataBindings.Add("Text", gv_DML.DataSource, "MaKH");

            txt_DMMaLop.DataBindings.Clear();
            txt_DMMaLop.DataBindings.Add("Text", gv_DML.DataSource, "MaLop");

            txt_DMSiSo.DataBindings.Clear();
            txt_DMSiSo.DataBindings.Add("Text", gv_DML.DataSource, "SiSo");

            txt_DMTenLop.DataBindings.Clear();
            txt_DMTenLop.DataBindings.Add("Text", gv_DML.DataSource, "TenLop");
        }
        private void simpleBt_Xoa_Click(object sender, EventArgs e)
        {
            bool kq;
            if (MessageBox.Show("Bạn có chắc sẽ xóa lớp "+txt_DMMaLop.Text+" hay không, nếu xóa thì toàn bộ thông thông tin liên quan sẽ mất.","Thông báo", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                kq = BUS_Lop.deleteLop(txt_DMMaLop.Text);
                if (!kq)
                    XtraMessageBox.Show("Xóa thất bại","Thông báo");
                else
                {
                    load();
                }
            }
            else
                load();
        }

        private void simpleBt_Them_Click(object sender, EventArgs e)
        {
            layoutKhoahoc_txt.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
            layoutKhoahoc_lookup.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
            simpleBt_Them.Enabled = false;
            simpleBt_Sua.Enabled = false;
            simpleBt_Xoa.Enabled = false;
            simpleBt_CapNhat.Enabled = true;
            simpleBt_Huy.Enabled = true;

            txt_DMHeDT.DataBindings.Clear();
            txt_DMMaKH.DataBindings.Clear();
            txt_DMMaLop.DataBindings.Clear();
            txt_DMSiSo.DataBindings.Clear();
            txt_DMTenLop.DataBindings.Clear();

            txt_DMHeDT.Text = "";
            txt_DMMaKH.Text = "";
            txt_DMMaLop.Text = "";
            txt_DMSiSo.Text = "";
            txt_DMTenLop.Text = "";

            txt_DMHeDT.Enabled = true;
            txt_DMMaKH.Enabled = true;
            txt_DMSiSo.Enabled = true;
            txt_DMMaLop.Enabled = true;
            txt_DMTenLop.Enabled = true;

            TTThemSua = true;
        }

        private void simpleBt_Huy_Click(object sender, EventArgs e)
        {
            load();
        }

        private void simpleBt_Sua_Click(object sender, EventArgs e)
        {
            layoutKhoahoc_txt.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
            layoutKhoahoc_lookup.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
            malop_cu = txt_DMMaLop.Text;
            simpleBt_Them.Enabled = false;
            simpleBt_Sua.Enabled = false;
            simpleBt_Xoa.Enabled = false;
            simpleBt_CapNhat.Enabled = true;
            simpleBt_Huy.Enabled = true;

            txt_DMHeDT.DataBindings.Clear();
            txt_DMMaKH.DataBindings.Clear();
            txt_DMMaLop.DataBindings.Clear();
            txt_DMSiSo.DataBindings.Clear();
            txt_DMTenLop.DataBindings.Clear();
       

            txt_DMHeDT.Enabled = true;
            txt_DMMaKH.Enabled = true;
            txt_DMSiSo.Enabled = true;
            txt_DMMaLop.Enabled = true;
            txt_DMTenLop.Enabled = true;

            TTThemSua = false;

        }
        public bool KTDulieu()
        {
            if (txt_DMMaLop.Text == "")
                return false;
            if (txt_DMSiSo.Text == "")
                return false;
            if (txt_DMHeDT.Text == "")
                return false;
            if (gridLookKhoahoc.EditValue == null)
                return false;
            if (txt_DMTenLop.Text == "")
                return false;
            return true;
            
        }
        
           
        private void simpleBt_CapNhat_Click(object sender, EventArgs e)
        {
            if (!KTDulieu())
            {
                XtraMessageBox.Show("Nhập đầy đủ nga", "Thông báo");
                return;
            }
            DTO_Lop lop = new DTO_Lop();
            lop.Malop = txt_DMMaLop.Text;
            lop.TenLop = txt_DMTenLop.Text;
            lop.SiSo = int.Parse( txt_DMSiSo.Text);
            lop.MaKH = gridLookKhoahoc.EditValue.ToString();
            lop.HeDT = txt_DMHeDT.Text;
            if (TTThemSua)
            {

                if (BUS_Lop.insertLop(lop))
                    load();
                else
                    XtraMessageBox.Show("lỗi từ hệ thống, hãy liên hệ với bộ phận kỹ thuật, mã lỗi [simpleBt_CapNhat_Click]", "Thông báo");
            }
            else
            {
                if (BUS_Lop.updateLop(lop,malop_cu))
                    load();
                else
                    XtraMessageBox.Show("lỗi từ hệ thống, hãy liên hệ với bộ phận kỹ thuật, mã lỗi [simpleBt_CapNhat_Click]", "Thông báo");
            }
        }

        private void grid_DMLop_Click(object sender, EventArgs e)
        {

        }

        private void gridLookKhoahoc_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void pictureEdit1_EditValueChanged(object sender, EventArgs e)
        {
            
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txt_DMMaLop_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                txt_DMTenLop.Focus();
        }

        private void txt_DMTenLop_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                txt_DMSiSo.Focus();
        }

        private void txt_DMSiSo_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                txt_DMMaKH.Focus();
        }

        private void txt_DMMaKH_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                txt_DMHeDT.Focus();
        }

        private void txt_DMHeDT_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                simpleBt_CapNhat.Focus();
        }

        private void txt_DMSiSo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar)
            && !char.IsDigit(e.KeyChar)
            && e.KeyChar != '.')
            {
                e.Handled = true;
            }


            if (e.KeyChar == '.'
                && (sender as TextBox).Text.IndexOf('.') > -1)
            {
                e.Handled = true;
            }

        }
    }
}